﻿$(document).ready(function () {        
    $('#tagList').tagSuggest({
        url: '/Tag/TagSuggestion',
        delay: 250
    });    

    $("#btnAddTag").click(function () {
        var tagName = jQuery.trim($("#tagName").val());
        if (tagName.length == 0) {
            alert("Please enter a tag name");
            return;
        }

        $.post("/Tag/Save", { id : null, name: tagName }, function (obj) {
            if (obj.Success == true) {
                //dynamically create the tag and add it to the span element
                $("#tagCheckboxes").append("<input type='checkbox' checked='checked' name='tag$" + obj.Result.ID + "' id='tag$" + obj.Result.ID + "'>" + obj.Result.Name + " ");
                $("#tagName").val("");
            }
            else {
                alert(obj.Message);
            }
        });
    });

    $("#btnAddCategory").click(function () {
        var categoryName = jQuery.trim($("#categoryName").val());
        if (categoryName.length == 0) {
            alert("Please enter a category name");
            return;
        }

        $.post("/Category/AddCategory", { name: categoryName }, function (obj) {
            if (obj.Success == true) {
                //dynamically create the tag and add it to the span element
                $("#tagCategories").append("<input type='checkbox' checked='checked' name='category$" + obj.Result.ID + "' id='category$" + obj.Result.ID + "'>" + obj.Result.Name + " ");
                $("#categoryName").val("");
            }
            else {
                alert(obj.Message);
            }
        });
    });    
});